//
//  BDHomeUserTableViewCell.m
//  BadBoy
//
//  Created by wen on 2017/3/21.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDHomeUserTableViewCell.h"
#import "BDHomeModel.h"
#import "BDTextLabel.h"

@interface BDHomeUserTableViewCell ()
{
    NSString *_currentText;
}
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *monthLabel;
@property (nonatomic, strong) UIView *lineview;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIImageView *yuanImage;
@property (nonatomic, strong) UIImageView *coverImage;
@property (nonatomic, strong) BDTextLabel *TextLab;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, copy) void(^clickBlock)();

@end

@implementation BDHomeUserTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    style = UITableViewCellStyleValue1;
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.dateLabel];
        [self.contentView addSubview:self.monthLabel];
        [self.contentView addSubview:self.lineview];
        [self.contentView addSubview:self.yuanImage];
        [self.contentView addSubview:self.bottomView];
        [self.contentView addSubview:self.coverImage];
        [self.contentView addSubview:self.TextLab];
    }
    return self;
}

- (void)configeWithModel:(id)model contentClick:(void(^)())contentClick{
    BDHomeModel *homeModel = model;
    if (homeModel.needDisplayDate == YES) {
        self.dateLabel.hidden = NO;
        self.monthLabel.hidden = NO;
        self.yuanImage.hidden = NO;
        NSDate *curDate = [NSDate dateWithTimeIntervalSince1970:homeModel.created_at.floatValue];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"MM-dd"];
        NSString *dateStr = [formatter stringFromDate:curDate];
        NSArray *dataArr = [dateStr componentsSeparatedByString:@"-"];
        self.dateLabel.text = [NSString stringWithFormat:@"%@", dataArr[1]];
        self.monthLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%@月"), dataArr[0]];
    }else{
        self.dateLabel.hidden = YES;
        self.monthLabel.hidden =YES;
        self.yuanImage.hidden = YES;
    }
    
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    //获取app中所有icon名字数组
    NSArray *iconsArr = infoDict[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"];
    //取最后一个icon的名字
    NSString *iconLastName = [iconsArr lastObject];
    NSString *imageName = homeModel.images.count ? homeModel.images.firstObject : @"business_pic";
    [self.coverImage sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:[UIImage imageNamed:iconLastName]];
    
    _currentText = homeModel.post;
    
    self.clickBlock = contentClick;
    
    if (homeModel.price != 0) {
        self.button.hidden = NO;
        self.button.selected = homeModel.isPurchase;
        self.button.backgroundColor = homeModel.isPurchase ? [UIColor colorWithHexString:@"#cccccc"] : [UIColor colorWithHexString:@"14cd67"];
    }else {
        self.button.hidden = YES;
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    [self.dateLabel sizeToFit];
    [self.monthLabel sizeToFit];
    self.dateLabel.left = 15;
    self.monthLabel.left = self.dateLabel.right;
    
    self.lineview.frame = CGRectMake(0, 0, 2, self.height);
    self.lineview.left = 80;
    
    self.yuanImage.size = CGSizeMake(10, 10);
    self.yuanImage.layer.masksToBounds = YES;
    self.yuanImage.layer.cornerRadius = 5;
    self.yuanImage.centerX = self.lineview.centerX;
    self.yuanImage.top = 30;
    
    self.monthLabel.centerY = self.yuanImage.centerY;
    self.dateLabel.bottom = self.monthLabel.bottom;
    
    self.bottomView.size = CGSizeMake(self.width - 5 - self.lineview.right - 10 - 20, self.height - 20);
    self.bottomView.right = self.width - 25;
    self.bottomView.centerY = self.height / 2.0;
    
    self.coverImage.size = CGSizeMake(80, 80);
    self.coverImage.left = self.bottomView.left;
    self.coverImage.top = self.bottomView.top;
    
    self.TextLab.numberOfLines = 0;
    self.TextLab.textColor = [UIColor colorWithHexString:@"#1c1c1c"];
    self.TextLab.lineBreakMode = NSLineBreakByWordWrapping;
    self.TextLab.width = self.bottomView.width - self.coverImage.width - 5;
    self.TextLab.height = self.bottomView.height;
    self.TextLab.right = self.bottomView.right;
    self.TextLab.centerY = self.bottomView.centerY;
    self.TextLab.textAlignment = NSTextAlignmentLeft;
    NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:[_currentText isNotEmpty] ? _currentText : @""];
    one.lineSpacing = 5;
    one.font = [UIFont systemFontOfSize:15];
    self.TextLab.attributedText = one;
    
    self.button.size = CGSizeMake(20, self.bottomView.height);
    self.button.top = self.bottomView.top;
    self.button.left = self.bottomView.right;
    
    if (self.cellType == BDHomeUserCellTypeDefault) {
        self.lineview.height = self.height;
        self.lineview.top = 0;
    }else if (self.cellType == BDHomeUserCellTypeFirst) {
        self.lineview.height = self.height - self.bottomView.top;
        self.lineview.top = self.bottomView.top;
    }else if (self.cellType == BDHomeUserCellTypeLast) {
        self.lineview.top = 0;
        self.lineview.height = self.bottomView.bottom;
    }else if (self.cellType == BDHomeUserCellTypeFirstAndLast) {
        self.lineview.top = self.bottomView.top;
        self.lineview.height = self.bottomView.height;
    }
}

- (UIButton *)button{
    if (!_button) {
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button setImage:[UIImage imageNamed:@"lock"] forState:UIControlStateNormal];
        [_button setImage:[UIImage imageNamed:@"lock_on"] forState:UIControlStateSelected];
        _button.userInteractionEnabled = NO;
        [self addSubview:_button];
    }
    return _button;
}

-(UILabel *)dateLabel{
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc]init];
        _dateLabel.textColor = KBDNameColor;
        _dateLabel.font = [UIFont boldSystemFontOfSize:18];
    }
    return _dateLabel;
}
-(UILabel *)monthLabel{
    if (!_monthLabel) {
        _monthLabel = [[UILabel alloc]init];
        _monthLabel.textColor = KBDNameColor;
        _monthLabel.font = [UIFont systemFontOfSize:12];
    }
    return _monthLabel;
}
-(UIView *)lineview{
    if (!_lineview) {
        _lineview = [[UIView alloc]init];
        _lineview.backgroundColor = kBDColorGray;
    }
    return _lineview;
}

-(UIImageView *)yuanImage{
    if (!_yuanImage) {
        _yuanImage = [[UIImageView alloc]init];
        _yuanImage.backgroundColor = [UIColor orangeColor];
    }
    return _yuanImage;
}

-(UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]init];
        _bottomView.backgroundColor = kBDTextBlackColorGray;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR)];
        [_bottomView addGestureRecognizer:tap];
    }
    return _bottomView;
}

- (void)tapGR{
    if (self.clickBlock) {
        self.clickBlock();
    }
}

-(UIImageView *)coverImage{
    if (!_coverImage) {
        _coverImage = [[UIImageView alloc]init];
        _coverImage.image = [UIImage imageNamed:@"KTV"];
    }
    return _coverImage;
}
-(BDTextLabel *)TextLab{
    if (!_TextLab) {
        _TextLab = [[BDTextLabel alloc]init];
        _TextLab.font = [UIFont systemFontOfSize:17];
        _TextLab.textColor = KBDNameColor;
        _TextLab.numberOfLines = 0;
    }
    return _TextLab;
}

@end
